package com.wandousoushu.jiusen.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wandousoushu.jiusen.R;
import com.wandousoushu.jiusen.booksource.bean.BookBean;
import com.wandousoushu.jiusen.booksource.data.entities.SearchBook;
import com.wandousoushu.jiusen.help.AppConfig;

import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.Vh>  {

    private Context mContext;
    private List<SearchBook> mSearchBookList;
    private LayoutInflater mInflater;
    private ActionListener mActionListener;
    private View.OnClickListener mOnClickListener;

    public SearchAdapter(Context mContext, List<SearchBook> searchBookList) {
        this.mContext = mContext;
        this.mSearchBookList=searchBookList;
        mInflater = LayoutInflater.from(mContext);
        this.notifyDataSetChanged();
        mOnClickListener=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Object tag = view.getTag();
                if(tag!=null){
                    int position = (int) tag;
                    if (mActionListener != null) {
                        mActionListener.onItemClick(mSearchBookList.get(position));
                    }
                }
            }
        };
    }

    public void setActionListener(ActionListener listener) {
        mActionListener = listener;
    }

    @Override
    public SearchAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_search_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(SearchAdapter.Vh holder, int position) {
        holder.setData(mSearchBookList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mSearchBookList.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        LinearLayout itemLinearLayout;
        TextView bookNameTextView;
        TextView bookAuthorTextView;
        TextView bookIntrodutionTextView;
        TextView originNameTextView;
        TextView originTextView;

        public Vh(View itemView) {
            super(itemView);
            itemLinearLayout=itemView.findViewById(R.id.item_ll_search);
            bookNameTextView=(TextView)itemView.findViewById(R.id.tv_search_book_name);
            bookAuthorTextView=(TextView)itemView.findViewById(R.id.tv_search_author);
            bookIntrodutionTextView=itemView.findViewById(R.id.tv_searck_introdution);
            originNameTextView = (TextView) itemView.findViewById(R.id.tv_search_origin_name);
            originTextView=(TextView)itemView.findViewById(R.id.tv_search_origin) ;
            itemView.setOnClickListener(mOnClickListener);
        }

        void setData(SearchBook searchBook, int position) {
            itemView.setTag(position);
            bookNameTextView.setText(searchBook.getName());
            bookAuthorTextView.setText(searchBook.getAuthor());
            bookIntrodutionTextView.setText(searchBook.getIntro());
            originNameTextView.setText(searchBook.getOriginName());
            originTextView.setText(searchBook.getOrigin());
        }
    }


    public void refresh(List<SearchBook> newList) {
        mSearchBookList.removeAll(mSearchBookList);
        mSearchBookList.addAll(newList);
        notifyDataSetChanged();
    }


    public interface ActionListener {
        void onItemClick(SearchBook searchBook);
    }
}
